package cn.yqn.pet.service.impl;

import cn.yqn.pet.domain.Pet;
import cn.yqn.pet.domain.PetDetail;
import cn.yqn.pet.mapper.PetDetailMapper;
import cn.yqn.pet.mapper.PetMapper;
import cn.yqn.pet.service.IPetService;
import cn.yqn.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yqn
 * @since 2022-08-06
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;


    @Override
    public void save(Pet pet) {
        // 先添加pet，返回自增的主键
        petMapper.save(pet);

        // 再添加pet_datail
        PetDetail petDetail = pet.getPetDetail();
        petDetail.setPet_id(pet.getId());
        petDetailMapper.save(petDetail);

    }

    @Override
    public void update(Pet pet) {
        // pet
        petMapper.update(pet);
        // detail
        petDetailMapper.update(pet.getPetDetail());
    }

    @Override
    public void onSale(List<Long> ids) {
        // 上架时间
        Date date = new Date();
        petMapper.onSale(ids, date);
    }

    @Override
    public void offSale(List<Long> ids) {
        // 下架时间
        Date date = new Date();
        petMapper.offSale(ids, date);
    }
}
